package com.example.hobbyplanetserve.Result;

import com.example.hobbyplanetserve.utils.ResultCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回结果类
 */
public class Result {

    private Boolean success;

    private Integer code;

    private String message;

    private Map<String,Object> data = new HashMap<>();

    public Boolean getSuccess() {return success;}

    public void setSuccess(Boolean success) {this.success = success;}

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    //把构造方法私有
    private Result(){}

    //成功静态方法
    public static Result ok(){
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }

    //自定义失败静态方法 要在GlobalExceptionHandler中自动添加code和message
    public static Result fail(Integer code,String message){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    //失败的静态方法
    public static Result error(){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("失败");
        return r;
    }

    //参数错误的静态方法
    public static Result paramError(){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.PARAM_ERROR);
        r.setMessage("参数错误");
        return r;
    }
    //请求数据空的静态方法
    public static Result dataEmpty(){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.NOT_FOUND);
        r.setMessage("请求数据为空");
        return r;
    }
    //未登录的静态方法
    public static Result notLogin(){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.NOT_LOGIN);
        r.setMessage("请求数据错误");
        return r;
    }

    //未授权的静态方法
    public static Result unauthorized(String message){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.NOT_AUTH);
        r.setMessage(message);
        return r;
    }

    //系统错误的静态方法
    public static Result systemError(String message){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.SYSTEM_ERROR);
        r.setMessage(message);
        return r;
    }

    //用户已存在的静态方法
    public static Result userExist(){
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.USER_EXIST);
        r.setMessage("用户已存在");
        return r;
    }

    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    public Result message(String message){
        this.setMessage(message);
        return this;
    }
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
    public Result data(Map<String,Object> data){
        this.setData(data);
        return this;
    }
    public Result data(String key,Object value){
        this.data.put(key, value);
        return this;
    }


}
